#Requires AutoHotkey v2.0
#Include FileTool.ahk

; SimLog(简单日志) v1.05
class SimLog {

    ; 存储路径位置的常量，经常使用 
    static PATH_FD := EnvGet("USERPROFILE") "\faintdream\"
    static PATH_FDL := EnvGet("USERPROFILE") "\faintdream\Log\"

    
    ; 日志是否启用(值为false的话,将不输出日志)
    enable := true

    ; 日志等级(1~6)
    level := 1

    ; 日志等级表(Map对象 等级对应关系 TRACE[1], DEBUG[2], INFO[3], WARN[4], ERROR[5], FATAL[6])
    ; llv := Map()

    ; 操作文件对象
    fileTool := FileTool()

    ; 日志路径(默认为当前路径下的SimLog.log文件)
    logPath := A_ScriptDir . "\SimLog.log"

    ; 默认换行符(New Line Character)
    nlch := "`r`n"

    ; 构造函数
    __New(logPath := this.logPath) {

        this.logPath := logPath

        ; 日志对象
        map1 := Map("lv", 1, "name", "TRACE", "enable", true)
        map2 := Map("lv", 2, "name", "DEBUG", "enable", true)
        map3 := Map("lv", 3, "name", "INFO", "enable", true)
        map4 := Map("lv", 4, "name", "WARN", "enable", true)
        map5 := Map("lv", 5, "name", "ERROR", "enable", true)
        map6 := Map("lv", 6, "name", "FATAL", "enable", true)

        this.llv := Map(map1["name"], map1,
            map2["name"], map2,
            map3["name"], map3,
            map4["name"], map4,
            map5["name"], map5,
            map6["name"], map6)
    }

    ; 输出日志(TRACE级别)
    trace(text, time := "") {
        this._log(text, this.llv["TRACE"]["lv"], time)
    }

    ; 输出日志(DEBUG级别)
    debug(text, time := "") {
        this._log(text, this.llv["DEBUG"]["lv"], time)
    }

    ; 输出日志(INFO级别)
    info(text, time := "") {
        this._log(text, this.llv["INFO"]["lv"], time)
    }

    ; 输出日志(WARN级别)
    warn(text, time := "") {
        this._log(text, this.llv["WARN"]["lv"], time)
    }

    ; 输出日志(ERROR级别)
    error(text, time := "") {
        this._log(text, this.llv["ERROR"]["lv"], time)
    }

    ; 输出日志(FATAL级别)
    fatal(text, time := "") {
        this._log(text, this.llv["FATAL"]["lv"], time)
    }

    ; 输出日志(log)
    ; 所有日志输出最后都将调用该函数实现
    ; 默认值log没有级别,只要this.enable值为true,就会有输出日志
    ; 第二个参数假如传入hide(隐藏)或者false
    log(text, level := "log", time := "") {
        
        if(level && level != "hide"){
            level := Format("[{1}]",level)
        }
        if (this.enable) {
            this.fileTool.write(this.logPath, Format("{1} {2}{3}{4}", text, level, this._getTime(time), this.nlch))
        }
    }

    ; 输出日志(实现)
    _log(text, level := 1, time := "" ) {

        lvName := "DEBUG"

        ; 相应日志等级的日志未启用
        For key in this.llv {
            if (level = this.llv[key]["lv"]) {
                if (this.llv[key]["enable"]) {
                    lvName := this.llv[key]["name"]
                } else { ; 相应日志等级的日志关闭
                    return
                }
            }
        }

        ; 日志未启用
        if (!this.enable) {
            return
        }

        ; 日志等级不符合要求
        if (this.level > level) {
            return
        }

        ; 输出日志
        ; this.fileTool.write(this.logPath, text . this._getTime(time) . this.nlch)
        ; this.fileTool.write(this.logPath, Format("[{1}] {2} {3}{4}", this.llv[lvName]["name"], text, this._getTime(time), this.nlch))
        this.log(text, lvName, time)
    }

    ; 获取时间(日期)
    _getTime(YYYYMMDDHH24MISS) {
        return "[" . formatTime(YYYYMMDDHH24MISS, "yyyy-MM-dd HH:mm:ss") . "]"
    }

}